package cst.neu.yuejian_31.model;

import java.util.List;

import cst.neu.yuejian_31.bean.CcourseBean;
import cst.neu.yuejian_31.iface.CcourseIface;
import cst.neu.yuejian_31.iface.CcourseListener;
import cst.neu.yuejian_31.service.CcourseService;
import cst.neu.yuejian_31.Common.Constant;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by xxe on 2017/10/18.
 */

public class CcourseModel implements CcourseIface {
    private Retrofit retrofit;
    public CcourseModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(Constant.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void getCcourse(String clubId,final CcourseListener ccourseListener) {
        CcourseService ccourseService=retrofit.create(CcourseService.class);
        Call<List<CcourseBean>>call=
                ccourseService.getCcourse(clubId);
        call.enqueue(new Callback<List<CcourseBean>>() {
            @Override
            public void onResponse(Call<List<CcourseBean>> call, Response<List<CcourseBean>> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    ccourseListener.onResponse(response.body());
                }else {
                    ccourseListener.onFail("FAIL");
                }
            }

            @Override
            public void onFailure(Call<List<CcourseBean>> call, Throwable t) {
                ccourseListener.onFail(t.toString());
            }
        });
    }
}
